#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
pthread_mutex_t lock;
pthread_mutexattr_t attr;
void *thread_function(void *arg)
{
    int *count = (int *)arg;
    for (int i = 0; i < *count; i++)
    {
        pthread_mutex_lock(&lock);
        printf("Thread %ld: Count = %d\n", pthread_self(), i);
        pthread_mutex_unlock(&lock);
    }
    return NULL;
}
int main()
{
    // 初始化属性对象
    pthread_mutexattr_init(&attr);
    // 设置为递归互斥锁
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    // 初始化互斥锁
    pthread_mutex_init(&lock, &attr);
    // 创建线程
    pthread_t thread1, thread2;
    int count = 5;
    pthread_create(&thread1, NULL, thread_function, &count);
    pthread_create(&thread2, NULL, thread_function, &count);
    // 等待线程结束
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    // 销毁互斥锁
    pthread_mutex_destroy(&lock);
    // 销毁属性对象
    pthread_mutexattr_destroy(&attr);
    return 0;
}